
//
//  StatusCellTopView.m
//  kuangjia
//
//  Created by 张帅 on 15/10/2.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//

#import "StatusCellTopView.h"

#import "UILabel+Contenlable.h"
#import <UIImageView+WebCache.h>

@interface StatusCellTopView ()

@property(nonatomic,strong) UIImageView *iconView;

@property(nonatomic,strong) UILabel *nameLabel;
@property(nonatomic,strong) UIImageView *memberView;
@property(nonatomic,strong) UILabel *timeLabel;
@property(nonatomic,strong) UILabel *sourceLabel;
@property(nonatomic,strong) UIImageView *vipView;

@end

@implementation StatusCellTopView


-(UIImageView *)iconView{
    if (_iconView==nil) {
        _iconView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
    }
    return _iconView;
}

-(UILabel *)nameLabel{
    
    if (_nameLabel==nil) {
        
        _nameLabel=[[UILabel alloc]initWithTitle:@"姓名" color:[UIColor darkGrayColor]sizeFont:13 ];
    }
    return _nameLabel;
}

-(UIImageView *)memberView{
    if (_memberView==nil) {
        _memberView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_membership_level1"]];
    }
    return _memberView;
}


-(UILabel *)timeLabel{
    
    if (_timeLabel==nil) {
        _timeLabel =[[UILabel alloc] initWithTitle:@"刚刚" color:[UIColor orangeColor] sizeFont:14];
    }
    return _timeLabel;
}

-(UILabel *)sourceLabel{
    if (_sourceLabel==nil) {
        _sourceLabel=[[UILabel alloc] initWithTitle:@"来自 新浪微博" color:[UIColor darkGrayColor] sizeFont:14];
    }
    return _sourceLabel;
}


-(UIImageView *)vipView{

    if (_vipView==nil) {
        _vipView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_grassroot"]];
    }
    return _vipView;

}


-(void)setStatusViewModel:(StatusViewModel *)statusViewModel{
    _statusViewModel=statusViewModel;
    // TODO: 设置界面数据
  
    self.nameLabel.text= statusViewModel.status.user.name;
    [self.iconView sd_setImageWithURL:statusViewModel.userIconUrl];
    self.vipView.image=statusViewModel.userVipImage;
    self.memberView.image=statusViewModel.userMemberImage;

    NSDate *date = [[NSDate alloc] init];
    
    date =[self sinaDateWithStr:self.statusViewModel.status.created_at];
    
    NSLog(@"%@",date);
    
    self.timeLabel.text = [self critedTime: date];
    
}


- (NSDate*)sinaDateWithStr:(NSString*)str{
    NSDateFormatter *dateformat= [[NSDateFormatter alloc] init];
    
            dateformat.locale =  [NSLocale localeWithLocaleIdentifier:@"en"];
    
            dateformat.dateFormat = @"EEE MMM dd HH:mm:ss zzz yyyy";
    
    
            return [dateformat dateFromString:str];
    
}

-(NSString *)critedTime:(NSDate*)created_at{

    NSLog(@"%@",created_at);
    

    NSCalendar *canlender  =[NSCalendar currentCalendar];

    // 2. 今天
    
    if ([canlender isDateInToday:created_at]) {
        // 计算当前系统时间距离指定时间的秒数

        
        NSDate *date1 =[[NSDate alloc] init];
        
        NSInteger delta = (NSInteger)[date1 timeIntervalSinceDate:created_at];
       
        if (delta<60) {
            
            return @"刚刚";
        }
        
        if (delta<3600) {
            
            
            return [NSString stringWithFormat:@"%ld分钟前",delta/60];
        
        }
        
        return [NSString stringWithFormat:@"%ld小时前",delta/3600];
        
    }
    // 3. 其他日期
    

    NSString *fmtString = @" HH:mm";
    
    if ([canlender isDateInYesterday:created_at]) {
     
        fmtString =[NSString stringWithFormat:@"昨天%@",fmtString];
        
    }else{
        // "MM-dd" + fmtString
        
        fmtString =[NSString stringWithFormat:@"MM-dd %@",fmtString];
        // 提取日期中指定`单位 year/month/day ....`的数字
        // print(canlender.component(.Year, fromDate: self))
        
        NSDateComponents *coms = [canlender components:NSCalendarUnitYear fromDate:created_at toDate:[NSDate date] options:0];
        
               // 一年前
        
        if (coms> 0) {
            fmtString =[NSString stringWithFormat:@"yyyy-%@",fmtString];

        }
    
    }
    
    // 根据格式字符串，生成对应的日期字符串

    NSDateFormatter *df =[[NSDateFormatter alloc] init];
    
    df.locale =[NSLocale localeWithLocaleIdentifier:@"en"];
    df.dateFormat =fmtString;

    return [df stringFromDate:created_at];
}




- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self=[super initWithCoder:aDecoder]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    // 1. 添加控件
    self.backgroundColor = [UIColor whiteColor];

    [self addSubview:self.iconView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.memberView];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.vipView];
    [self addSubview:self.timeLabel];
      //MARK: iconView 的布局
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(HMStatusCellMargin);
        
        make.top.equalTo(self.mas_top).offset(HMStatusCellMargin);
        make.width.equalTo(@(HMStatusIconWidth));
        make.height.equalTo(@(HMStatusIconWidth));
    }];
      // MARK: nameLable
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(HMStatusCellMargin);
        make.top.equalTo(self.mas_top).offset(HMStatusCellMargin);
     
    }];
    

   // MARK: memberLable
    
    [self.memberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(HMStatusCellMargin);
        
        make.top.equalTo(self.iconView.mas_top);
        
    }];
    


    // MARK: memberLable
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(HMStatusCellMargin);
        make.top.equalTo(self.nameLabel.mas_bottom);
        
    }];
    

    // MARK: sourceLable
    
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.timeLabel.mas_right).offset(HMStatusCellMargin);
        make.top.equalTo(self.timeLabel.mas_top);
        
    }];

      // MARK: VipView
    [self.vipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(-HMStatusCellMargin);
        make.top.equalTo(self.iconView.mas_bottom).offset(-HMStatusCellMargin);
    }];
   
    
}


@end
